package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zhentao.common.enums.admin.AdManner;
import com.zhentao.common.enums.admin.AdWithdrawal;
import com.zhentao.common.enums.dto.AdAdWithdrawalDto;
import com.zhentao.dtos.Result;
import com.zhentao.helper.ThreadLocalUtil;
import com.zhentao.mapper.AdMannerMapper;
import com.zhentao.mapper.AdWithdrawalMapper;
import com.zhentao.service.AdWithdrawalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 小明
* @description 针对表【ad_withdrawal】的数据库操作Service实现
* @createDate 2024-09-06 08:56:49
*/
@Service
public class AdWithdrawalServiceImpl extends ServiceImpl<AdWithdrawalMapper, AdWithdrawal>
    implements AdWithdrawalService {

    @Autowired
    private AdWithdrawalMapper adWithdrawalMapper;
    @Autowired
    private AdMannerMapper adMannerMapper;

    @Override
    public Result AdWithdrawalList(AdAdWithdrawalDto adWithdrawalDto) {
        Page<AdWithdrawal> page = new Page<>(adWithdrawalDto.getPage(),adWithdrawalDto.getSize());
        LambdaQueryWrapper<AdWithdrawal> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(adWithdrawalDto.getStart()!=null, AdWithdrawal::getRequestDate, adWithdrawalDto.getRequestDate())
                .le(adWithdrawalDto.getEnd()!=null,AdWithdrawal::getRequestDate,adWithdrawalDto.getEnd())
                .orderByDesc(AdWithdrawal::getRequestDate);
        Page<AdWithdrawal> adWithdrawalPage = adWithdrawalMapper.selectPage(page, wrapper);

        for (AdWithdrawal record : adWithdrawalPage.getRecords()) {
            AdManner adManner = adMannerMapper.selectById(record.getMannerId());
            record.setMannerName(adManner.getMName());
        }


        return Result.success().put("data", adWithdrawalPage.getRecords()).put("total", adWithdrawalPage.getTotal());
    }

    @Override
    public Result AdWithdrawalStatus(AdWithdrawal adWithdrawal) {

        if (adWithdrawal.getWithdrawalId()==null){
            return Result.error("请选择要审批的数据");
        }
        AdWithdrawal selectById = adWithdrawalMapper.selectById(adWithdrawal.getWithdrawalId());
        if (selectById!=null){
            if (selectById.getStatus() == 0 || selectById.getStatus() == 1){
                selectById.setStatus(2);
                selectById.setRejectionReason(adWithdrawal.getRejectionReason());
                selectById.setApprovalBy(Integer.valueOf(ThreadLocalUtil.get().toString()));
                selectById.setApprovalDate(new Date());
            }else {
                selectById.setStatus(1);
                selectById.setApprovalBy(Integer.valueOf(ThreadLocalUtil.get().toString()));
                selectById.setApprovalDate(new Date());
            }
        }
        int i = adWithdrawalMapper.updateById(selectById);
        if (i == 1){
            return Result.success("操作成功");
        }else {
            return Result.error("操作失败");
        }
    }


}




